import { Component, ChangeDetectorRef, ChangeDetectionStrategy } from "@angular/core";
import { Subscription } from "rxjs";
import { CompanyService } from "../../company.service";
import { CompanyGovernanceService } from "../companyGovernance.service";
import { OverallPerformanceModel } from "../models/overallPerformance.model";
import { AssessmentList } from "../../../../../shared/models/assessment.enum";
import { ActivatedRoute, Router } from "@angular/router";
import { CompanyBaseWidget } from "../../shared/companyBaseWidget/companyBase.widget";

/**
 * Governance widget component
 * 
 * Author: Sorin Spiru
 * Date: 04.03.2018
 */
@Component({
    moduleId: module.id,
    templateUrl: './widget.component.html',
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class WidgetComponent extends CompanyBaseWidget {
    private companySub: Subscription;
    private govSub: Subscription;
    public overallPerformance: OverallPerformanceModel;
    public assessmentList: typeof AssessmentList = AssessmentList;

    constructor(
        private cdRef: ChangeDetectorRef,
        private route: ActivatedRoute,
        private router: Router,
        private companyService: CompanyService,
        private companyGovernanceService: CompanyGovernanceService) {
        super(route, router);
    }

    ngOnInit() {
        this.companySub = this.companyService.company.subscribe(company => {            
            this.companyGovernanceService.getGovernanceOverallPerformance(company.id);
            this.govSub = this.companyGovernanceService.overallPerformance.subscribe(data => {
                this.overallPerformance = data;
                this.cdRef.markForCheck();
            });
        });
    }

    ngOnDestroy() {
        this.companySub.unsubscribe();
        this.govSub.unsubscribe();
    }
}